Data importation and cleaning

This data set was generated by Reddit user yuxbni76 https://www.reddit.com/user/yuxbni76 The data was scraped from ESPN.com and covers the 2002 to 2019 seasons up to week 6. Three games were missing in the data set (kind of weird they are all near holidays).

So I found that data on https://www.pro-football-reference.com/ and added it into the data set before importing.

nfl_stats<- read.csv("nfl_dataset_2002-2019week6.csv")
# Convert columns that make sense to factor
## Home and away team names
nfl_stats$home <- as.factor(nfl_stats$home)
nfl_stats$away <- as.factor(nfl_stats$away)

Team colors

Team colors were extracted from https://teamcolorcodes.com, I took the first primary color for each team and created a list that will be for later use. For the Browns and Titans I took the secondary color as it seemed more appropriate.

Team_colors <- c("49ers"="#AA0000",
                 "Bears"="#0B162A",
                 "Bengals"="#FB4F14",
                 "Bills"="#00338D",
                 "Broncos"="#FB4F14",
                 "Browns"="#FF3C00",
                 "Buccaneers"="#D50A0A",
                 "Cardinals"="#97233F",
                 "Chargers"="#0080C6",
                 "Chiefs"="#E31837",
                 "Colts"="#002C5F",
                 "Cowboys"="#041E42",
                 "Dolphins"="#008E97",
                 "Eagles"="#004C54",
                 "Falcons"="#A71930",
                 "Giants"="#0B2265",
                 "Jaguars"="#006778",
                 "Jets"="#125740",
                 "Lions"="#0076B6",
                 "Packers"="#203731",
                 "Panthers"="#0085CA",
                 "Patriots"="#002244",
                 "Raiders"="#000000",
                 "Rams"="#003594",
                 "Ravens"="#241773",
                 "Redskins"="#773141",
                 "Saints"="#D3BC8D",
                 "Seahawks"="#002244",
                 "Steelers"="#FFB612",
                 "Texans"="#03202F",
                 "Titans"="#4B92DB",
                 "Vikings"="#4F2683")

Passing Yards

nfl_stats$game_number <- seq(1,length(nfl_stats$date), 1)
P <- ggplot(nfl_stats, aes(x=game_number, y=passing_yards_home, color = home, label=date, label2=score_home, label3=score_away)) + geom_smooth(se=FALSE) + scale_color_manual(values = Team_colors) +
  geom_point() +
  theme(axis.ticks.x = element_blank(),
        axis.text.x = element_blank(),
        axis.title.x = element_blank()) +
    annotate("text", x = 134, y = 520, label = "2002", vjust = -0.5) +
    annotate("text", x = 4405, y = 520, label = "2018", vjust = -0.5) 

Q <- ggplot(nfl_stats, aes(x=game_number, y=passing_yards_away, color = away, label=date, label2=score_home, label3=score_away)) + geom_smooth(se=FALSE) + scale_color_manual(values = Team_colors) +
  geom_point()

R <- ggplot(nfl_stats, aes(x=game_number, y=rushing_yards_home, color = home, label=date, label2=score_home, label3=score_away)) + geom_smooth(se=FALSE) + scale_color_manual(values = Team_colors) +
  geom_point()

S <- ggplot(nfl_stats, aes(x=game_number, y=rushing_yards_away, color = away, label=date, label2=score_home, label3=score_away)) + geom_smooth(se=FALSE) + scale_color_manual(values = Team_colors) +
  geom_point()


plot <- ggplotly(P, tooltip = c('passing_yards_home', 'home', 'date','score_home','score_away'))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
plot[['x']][['layout']][['shapes']] <- c()

plot <- layout(plot,shapes = list(list(type = "rect", fillcolor = "black", line = list(color = "black"), opacity = 0.25, x0 = 1, x1 = 267, xref = "x", y0 = -100, y1 = 550, yref = "y"),
list(type = "rect", fillcolor = "black", line = list(color = "black"), opacity = 0.25, x0 = 534, x1 = 801, xref = "x", y0 = -100, y1 = 550, yref = "y"),
list(type = "rect", fillcolor = "black", line = list(color = "black"), opacity = 0.25, x0 = 1068, x1 = 1335, xref = "x", y0 = -100, y1 = 550, yref = "y"),
list(type = "rect", fillcolor = "black", line = list(color = "black"), opacity = 0.25, x0 = 1602, x1 = 1869, xref = "x", y0 = -100, y1 = 550, yref = "y"),
list(type = "rect", fillcolor = "black", line = list(color = "black"), opacity = 0.25, x0 = 2136, x1 = 2403, xref = "x", y0 = -100, y1 = 550, yref = "y"),
list(type = "rect", fillcolor = "black", line = list(color = "black"), opacity = 0.25, x0 = 2670, x1 = 2937, xref = "x", y0 = -100, y1 = 550, yref = "y"),
list(type = "rect", fillcolor = "black", line = list(color = "black"), opacity = 0.25, x0 = 3204, x1 = 3471, xref = "x", y0 = -100, y1 = 550, yref = "y"),
list(type = "rect", fillcolor = "black", line = list(color = "black"), opacity = 0.25, x0 = 3738, x1 = 4005, xref = "x", y0 = -100, y1 = 550, yref = "y"),
list(type = "rect", fillcolor = "black", line = list(color = "black"), opacity = 0.25, x0 = 4272, x1 = 4539, xref = "x", y0 = -100, y1 = 550, yref = "y")))
plot
#ggplotly(R, tooltip = c('rushing_yards_home', 'away', 'date','score_home','score_away'))

#ggplotly(Q, tooltip = c('passing_yards_away', 'away', 'date','score_home','score_away'))
#ggplotly(S, tooltip = c('rusing_yards_away', 'away', 'date','score_home','score_away'))